<?php
namespace app\servergoods\logic;

use \app\manage\logic\BaseLogic;
use \app\manage\service\ApiBase;


class Goods extends BaseLogic
{

	protected $goodsLogic = 'app\servergoods\logic\Goods';
    protected $goodsValidate = 'app\servergoods\validate\Goods';

    use \app\serveruser\traits\RunOneAddUserInfo;
    use \app\serveruser\traits\AddAppIdToRunOne;

    //获取商品详情
    public function goodsAllDetail($data): array 
    {
        $getAppInfo = $this->_getAppId($data, true, $this->goodsValidate, $this->goodsLogic);
        if($getAppInfo['status'] != SUCCESS_STATUS){
            return $getAppInfo;
        }
        $data['app_id'] = $getAppInfo['res']['id'];
    	return $this->_runOneAddOneUserInfo($data, 'servergoods/goods/Goods/managerGoodsDetail', $this->goodsValidate, $this->goodsLogic);
    }
    //管理员获取商品列表
    public function adminGoodsList($data): array 
    {
        $getAppInfo = $this->_getAppId($data, true, $this->goodsValidate, $this->goodsLogic);
        if($getAppInfo['status'] != SUCCESS_STATUS){
            return $getAppInfo;
        }
        $data['app_id'] = $getAppInfo['res']['id'];
    	return $this->_runOneAddLotsUserInfo($data, 'servergoods/goods/Goods/adminGoodsList', $this->goodsValidate, $this->goodsLogic);
    }

    /*艺货管理员发布商品*/
    public function yihuoAddGoods($data): array 
    {
        $service = new ApiBase($data['token']);
        $app_key = \app\manage\service\ApiBase::getAppKey();
        //获取user_id app_id
        $getBase['query'] = json_encode([
            ['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]],
            ['path'=>'serveruser/auth/App/find', 'param'=>['app_key'=>$app_key]]
        ]);
        $baseInfo = $service->sendDataLots(config('proxy_all_url'), $getBase);
        if($baseInfo['status'] != SUCCESS_STATUS){
            return $baseInfo;
        }
        //获取商品编码
        $getCode['query'] = json_encode([
            ['path'=>'servercoding/coding/Coding/getCoding', 'param'=>['type'=>'goods','token'=>$data['token'],'app_id'=>$baseInfo['res'][1]['id']]]
        ]);
        $codeInfo = $service->sendDataOne(config('proxy_all_url'), $getCode);
        if($codeInfo['status'] != SUCCESS_STATUS){
            return $codeInfo;
        }
        //发布商品
        $data['user_id'] = $baseInfo['res'][0]['id'];
        $data['app_id'] = $baseInfo['res'][1]['id'];
        $data['product_code'] = $codeInfo['res']['autoRes'];
        
        $addGoods['query'] = json_encode([
            ['path'=>'servergoods/goods/Goods/yihuoAddGoods', 'param'=>$data]
        ]);
        return $service->sendDataOne(config('proxy_all_url'), $addGoods);
    }

    /*艺货后台管理员 - 编辑某个商品*/
    public function yihuoEditGoods($data): array 
    {
        $service = new ApiBase($data['token']);
        $app_key = \app\manage\service\ApiBase::getAppKey();
        //获取user_id app_id
        $getBase['query'] = json_encode([
            ['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]],
            ['path'=>'serveruser/auth/App/find', 'param'=>['app_key'=>$app_key]]
        ]);
        $baseInfo = $service->sendDataLots(config('proxy_all_url'), $getBase);
        if($baseInfo['status'] != SUCCESS_STATUS){
            return $baseInfo;
        }
        $data['user_id'] = $baseInfo['res'][0]['id'];
        $data['app_id'] = $baseInfo['res'][1]['id'];
        $editGoods['query'] = json_encode([
            ['path'=>'servergoods/goods/Goods/yihuoEditGoods', 'param'=>$data]
        ]);
        return $service->sendDataOne(config('proxy_all_url'), $editGoods);

    }

    /*艺货 - 添加一条与艺术家的账单*/
    public function addAccountBook($data): array 
    {
        //获取用户信息
        $getBase['query'] = json_encode([
            ['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]]
        ]);
        $baseInfo = ApiBase::sendDataOne(config('proxy_all_url'), $getBase);
        if($baseInfo['status'] != SUCCESS_STATUS){
            return $baseInfo;
        }
        $data['create_user_info'] = $baseInfo['res'];
        $addAccountBook['query'] = json_encode([
            ['path'=>'servergoods/goods/Goods/addAccountBook', 'param'=>$data]
        ]);
        return ApiBase::sendDataOne(config('proxy_all_url'), $addAccountBook);
    }

    /*艺货 - 标注该账单已结算*/
    public function signSettlement($data): array 
    {
        //获取用户信息
        $getBase['query'] = json_encode([
            ['path'=>'serveruser/auth/Token/hGetAll', 'param'=>['token'=>$data['token']]]
        ]);
        $baseInfo = ApiBase::sendDataOne(config('proxy_all_url'), $getBase);
        if($baseInfo['status'] != SUCCESS_STATUS){
            return $baseInfo;
        }
        $data['sign_settlement_user_info'] = $baseInfo['res'];
        $signSettlement['query'] = json_encode([
            ['path'=>'servergoods/goods/Goods/signSettlement', 'param'=>$data]
        ]);
        return ApiBase::sendDataOne(config('proxy_all_url'), $signSettlement);
    }
    
}
